package com.snail.controller;

import com.snail.VO.ResultVO;
import com.snail.convert.OrderForm2OrderDTOConverter;
import com.snail.dto.OrderDto;
import com.snail.enums.ResultEnum;
import com.snail.exception.SellException;
import com.snail.form.OrderForm;
import com.snail.service.BuyerService;
import com.snail.service.OrderService;
import com.snail.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 【swg】.
 * @Date 2017/12/8 15:26
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    @RequestMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult br){
        //先进行数据的验证
        if(br.hasErrors()){log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        //这里还要判断一下购物车是不是为空，就是items中有没有值
        //这里的items是一个list，如果死List，就好处理了。。。
        OrderDto orderDto = OrderForm2OrderDTOConverter.convert(orderForm);
        if(StringUtils.isEmpty(orderDto.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDto create = orderService.create(orderDto);
        Map<String,String> map = new HashMap<>();
        map.put("orderId",create.getOrderId());
        return ResultVOUtil.success(map);
    }


    @RequestMapping("/list")
    public ResultVO<List<OrderDto>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){
        if (StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDto> orderDtoPage = orderService.findListByOpenid(openid,pageRequest);
        return ResultVOUtil.success(orderDtoPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVO detail(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderid) {
        OrderDto orderDTO = buyerService.findOrderOne(openid, orderid);
        return ResultVOUtil.success(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderid) {
        buyerService.cancelOrder(openid, orderid);
        return ResultVOUtil.success();
    }
}
